package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.Elevator;
import com.example.entity.PointDefend;
import com.example.entity.PointFlow;
import com.example.entity.authority.Party;
import com.example.entity.authority.PartyContract;
import com.example.entity.authority.PartyUser;
import com.example.ext.entity.ExtParty;
import com.example.mapp.*;
import com.example.service.PartyService;
import com.example.util.DateUtils;
import com.example.util.ExcelUtil;
import com.example.util.ObjectBeanUtils;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * Created by youkun on 2017/9/9.
 */
@Service
public class PartyServiceImpl  implements PartyService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(PartyServiceImpl.class);

	@Autowired
	private PartyMapp partyMapp;

	@Autowired
	private PromotionMapp promotionMapp;

	@Autowired
	private PartyUserMapp partyUserMapp;

	@Autowired
	private PartyContractMapp partyContractMapp;

	@Autowired
	private ElevatorMapp elevatorMapp;

	// 保存流水
	@Autowired
	private PointFlowMapp pointFlowMapp;

    @Override
    public Map<String, Object> queryList(ExtParty extParty) {
        List<ExtParty> list =null;
        Map<String,Object> map = new HashMap<String,Object>();
        try {
        	/**
			 * 判断是否分页
        	 */
        	if(extParty.getCurrentPage() == null){
				list = partyMapp.queryList(extParty);
				map.put("list",list);
			}else{
				Page<ExtParty> page = PageHelper.startPage(extParty.getCurrentPage(), extParty.getPageSize());
				list = partyMapp.queryList(extParty);
				map.put("list",list);
				map.put("total",page.getTotal());
			}

        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",map);
    }

	@Override
	public Map<String, Object> queryRandomList(Map map) {
		List<Map> list =null;
		Map<String,Object> map1 = new HashMap<String,Object>();
		try {
			Page<Map> page = PageHelper.startPage(
					Integer.parseInt(map.get("currentPage")+""),
					Integer.parseInt(map.get("pageSize")+""));
			list = partyMapp.queryRandomList(map);
			map1.put("list",list);
			map1.put("total",page.getTotal());

		} catch (Exception e) {
			logger.error(e.getMessage());
			return ResultUtil.returnError("查询失败"+e.getMessage(),null);
		}
		return ResultUtil.returnSuccess("查询成功",map1);
	}

    @Override
	@Transactional
    public Map<String, Object> add(Party party) {
        try {
            Party sys = partyMapp.queryByName(party.getPartyName());
            if(null==sys){
                int i = partyMapp.insert(party);
//				if ("1".equals(party.getPartyType()) || "2".equals(party.getPartyType())) {
//					Promotion promotion = new Promotion();
//					promotion.setType(party.getPartyType());
//					promotion.setUserId(party.getId() + "");
//					PromotionParty promotionParty = new PromotionParty();
//					promotionParty.setUserId(party.getId() + "");
//					promotionParty.setType(party.getPartyType());
//					Integer empiricValueAdded = 0;
//					if (null != party.getCorporation()) {
//						promotionParty.setCorporation("1");
//						empiricValueAdded += 100;
//						PromotionRecord promotionRecord = new PromotionRecord();
//						promotionRecord.setUserId(party.getId() + "");
//						promotionRecord.setType(party.getPartyType());
//						promotionRecord.setEmpiricItem("法人代表信息");
//						promotionRecord.setAddedTime(PromotionUtil.getTime());
//						promotionRecord.setEmpiricValue(100);
//						promotionMapp.insertRecord(promotionRecord);
//					}else{
//                        promotionParty.setCorporation("0");
//                    }
//					if (null != party.getTelephone()) {
//						promotionParty.setTelephone("1");
//						empiricValueAdded += 100;
//						PromotionRecord promotionRecord = new PromotionRecord();
//						promotionRecord.setUserId(party.getId() + "");
//						promotionRecord.setType(party.getPartyType());
//						promotionRecord.setEmpiricItem("单位电话信息");
//						promotionRecord.setAddedTime(PromotionUtil.getTime());
//						promotionRecord.setEmpiricValue(100);
//						promotionMapp.insertRecord(promotionRecord);
//					}else{
//                        promotionParty.setTelephone("0");
//                    }
//					if (null != party.getContacts()) {
//						promotionParty.setContacts("1");
//						empiricValueAdded += 100;
//						PromotionRecord promotionRecord = new PromotionRecord();
//						promotionRecord.setUserId(party.getId() + "");
//						promotionRecord.setType(party.getPartyType());
//						promotionRecord.setEmpiricItem("联系人信息");
//						promotionRecord.setAddedTime(PromotionUtil.getTime());
//						promotionRecord.setEmpiricValue(100);
//						promotionMapp.insertRecord(promotionRecord);
//					}else{
//                        promotionParty.setContacts("0");
//                    }
//					if (null != party.getContactsWork()) {
//						promotionParty.setContactsWork("1");
//						empiricValueAdded += 100;
//						PromotionRecord promotionRecord = new PromotionRecord();
//						promotionRecord.setUserId(party.getId() + "");
//						promotionRecord.setType(party.getPartyType());
//						promotionRecord.setEmpiricItem("联系人职务信息");
//						promotionRecord.setAddedTime(PromotionUtil.getTime());
//						promotionRecord.setEmpiricValue(100);
//						promotionMapp.insertRecord(promotionRecord);
//					}else{
//                        promotionParty.setContactsWork("0");
//                    }
//					if (null != party.getContactsCard()) {
//						promotionParty.setContactsCard("1");
//						empiricValueAdded += 100;
//						PromotionRecord promotionRecord = new PromotionRecord();
//						promotionRecord.setUserId(party.getId() + "");
//						promotionRecord.setType(party.getPartyType());
//						promotionRecord.setEmpiricItem("联系人身份证号码信息");
//						promotionRecord.setAddedTime(PromotionUtil.getTime());
//						promotionRecord.setEmpiricValue(100);
//						promotionMapp.insertRecord(promotionRecord);
//					}else{
//                        promotionParty.setContactsCard("0");
//                    }
//					if (null != party.getContactsPhone()) {
//						promotionParty.setContactsPhone("1");
//						empiricValueAdded += 100;
//						PromotionRecord promotionRecord = new PromotionRecord();
//						promotionRecord.setUserId(party.getId() + "");
//						promotionRecord.setType(party.getPartyType());
//						promotionRecord.setEmpiricItem("联系人电话信息");
//						promotionRecord.setAddedTime(PromotionUtil.getTime());
//						promotionRecord.setEmpiricValue(100);
//						promotionMapp.insertRecord(promotionRecord);
//					}else{
//                        promotionParty.setContactsPhone("0");
//                    }
//					if (null != party.getContactsEmile()) {
//						promotionParty.setContactsEmile("1");
//						empiricValueAdded += 100;
//						PromotionRecord promotionRecord = new PromotionRecord();
//						promotionRecord.setUserId(party.getId() + "");
//						promotionRecord.setType(party.getPartyType());
//						promotionRecord.setEmpiricItem("联系人邮箱信息");
//						promotionRecord.setAddedTime(PromotionUtil.getTime());
//						promotionRecord.setEmpiricValue(100);
//						promotionMapp.insertRecord(promotionRecord);
//					}else{
//                        promotionParty.setContactsEmile("0");
//                    }
//					if (null != party.getContactsMobile()) {
//						promotionParty.setContactsMobile("1");
//						empiricValueAdded += 100;
//						PromotionRecord promotionRecord = new PromotionRecord();
//						promotionRecord.setUserId(party.getId() + "");
//						promotionRecord.setType(party.getPartyType());
//						promotionRecord.setEmpiricItem("联系人手机信息");
//						promotionRecord.setAddedTime(PromotionUtil.getTime());
//						promotionRecord.setEmpiricValue(100);
//						promotionMapp.insertRecord(promotionRecord);
//					}else{
//                        promotionParty.setContactsMobile("0");
//                    }
//					if (null != party.getContactsAddress()) {
//						promotionParty.setContactsAddress("1");
//						empiricValueAdded += 100;
//						PromotionRecord promotionRecord = new PromotionRecord();
//						promotionRecord.setUserId(party.getId() + "");
//						promotionRecord.setType(party.getPartyType());
//						promotionRecord.setEmpiricItem("联系人地址信息");
//						promotionRecord.setAddedTime(PromotionUtil.getTime());
//						promotionRecord.setEmpiricValue(100);
//						promotionMapp.insertRecord(promotionRecord);
//					}else{
//                        promotionParty.setContactsAddress("0");
//                    }
//					promotionMapp.insertParty(promotionParty);
//					promotion.setEmpiricValue(empiricValueAdded);
//					promotionMapp.insert(promotion);
//					// 记录增加的积分值
//					PromotionPartyAdded promotionPartyAdded = new PromotionPartyAdded();
//					promotionPartyAdded.setMonth(new SimpleDateFormat("yyyyMM").format(new Date()));
//					promotionPartyAdded.setUserId(party.getId() + "");
//					promotionPartyAdded.setEmpiricValueAdded(empiricValueAdded);
//					promotionMapp.insertPartyAdded(promotionPartyAdded);
//				}
            }else {
                throw new Exception(party.getPartyName()+"已存在!");
            }

        }catch (Exception e){
        	TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

    @Override
    @Transactional
    public Map<String, Object> update(Party party) {
        try {
            //查询数据是否存在
            ExtParty system = partyMapp.queryById(party.getId().toString());
            if(null==system){
				return ResultUtil.returnError(party.getPartyName()+"不存在，请联系管理员!",null);
            }
            if(party.getPartyName().equals(system.getPartyName())){
                partyMapp.update(party);
            }else{
                Party sys = partyMapp.queryByName(party.getPartyName());
                if(null==sys){
                    partyMapp.update(party);
                }else {
					return ResultUtil.returnError(party.getPartyName()+"已存在!",null);
                }
            }
        }catch (Exception e){
        	TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage(),e);
            return ResultUtil.returnError("更新失败！",null);
        }finally {
			// 监管不计算
			if (!"4".equals(party.getLoggerType())) {
				// 每一次操作都会进行判断 ,通过唯一标识获取配置信息
				PointDefendInfoCache infoCache = new PointDefendInfoCache();
				PointDefend pointDefend = infoCache.getKeyByValue("unit_institutional_information");
				if (pointDefend != null) {
					// 获取必填字段
					List<String> strings = Arrays.asList(pointDefend.getMustBean().split(","));

					double i = ObjectBeanUtils.percentage(party, strings);
					if (i != 0) {

						// 查询角色信息
						Map<String, Object> maps = pointFlowMapp.findUserInfo(Long.valueOf(party.getLoggerPartyId()));
						// 计算分数
						int round = (int) (i * pointDefend.getMark());
						// 判断原来的分数是否为 0
						long mark = (long) maps.get("mark");
						if (mark != 0) {
							// 通过唯一标识查询流水表获取上一次积分，然后减去
							PointFlow pointFlow = new PointFlow();
							pointFlow.setIdentification("unit_institutional_information");
							List<PointFlow> list = pointFlowMapp.findOne(pointFlow);
							if (list != null && list.size() > 0) {
								round = (int) ((mark - list.get(0).getMark()) + round);
							} else {
								round = (int) (mark + round);
							}
						}
						String userId = (String) maps.get("userId");
						int i1 = pointFlowMapp.updateMark(round, userId);
						//
						if (i1 != 0) {
							// 插入流水信息
							PointFlow flow = new PointFlow();
							flow.setOperationType(pointDefend.getOperationType());
							flow.setOperationContent(pointDefend.getOperationContent());
							flow.setUserName((String) maps.get("userName"));
							flow.setPartyName((String) maps.get("partyName"));
							flow.setIdentification(pointDefend.getIdentification());
							flow.setMark(round);
							flow.setFunction("完善本单位的机构信息");
							flow.setCreateBy((String) maps.get("userName"));
							flow.setCreateDate(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
							pointFlowMapp.insertPointFlow(flow);

						}
					}
				}

			}
		}
        return ResultUtil.returnSuccess("成功",null);
    }

//    private void calcParty(Party party, String content) throws Exception {
//    	Map<String, Object> map = new HashMap<>();
//		map.put("type", party.getPartyType());
//		map.put("userId", String.valueOf(party.getId()));
//		Promotion promotion = promotionMapp.findById(map);
//		if (null == promotion) {
//			promotion = new Promotion();
//			promotion.setType(party.getPartyType());
//			promotion.setUserId(party.getId() + "");
//			PromotionParty promotionParty = new PromotionParty();
//			promotionParty.setUserId(party.getId() + "");
//			promotionParty.setType(party.getPartyType());
//			Integer empiricValueAdded = 0;
//			promotionParty.setZzjb("1");
//			empiricValueAdded += 5;
//			PromotionRecord promotionRecord = new PromotionRecord();
//			promotionRecord.setUserId(party.getId() + "");
//			promotionRecord.setType(party.getPartyType());
//			promotionRecord.setEmpiricItem("在单位信息注册中填写单位资质等级");
//			promotionRecord.setAddedTime(PromotionUtil.getTime());
//			promotionRecord.setEmpiricValue(5);
//			promotionMapp.insertRecord(promotionRecord);
//			promotionMapp.insertParty(promotionParty);
//			promotion.setEmpiricValue(empiricValueAdded);
//			promotionMapp.insert(promotion);
//			// 记录增加的积分值
//			PromotionPartyAdded promotionPartyAdded = new PromotionPartyAdded();
//			promotionPartyAdded.setMonth(new SimpleDateFormat("yyyyMM").format(new Date()));
//			promotionPartyAdded.setUserId(party.getId() + "");
//			promotionPartyAdded.setEmpiricValueAdded(empiricValueAdded);
//			promotionMapp.insertPartyAdded(promotionPartyAdded);
//			return;
//		}
//		PromotionParty promotionParty = promotionMapp.findPartyById(party.getId());
//		String month = new SimpleDateFormat("yyyyMM").format(new Date());
//		Map<String, Object> map2 = new HashMap<>();
//		map2.put("month", month);
//		map2.put("userId", String.valueOf(party.getId()));
//		PromotionPartyAdded promotionPartyAdded = promotionMapp.findPartyAddedById(map2);
//
//		Integer empiricValueAdded = 0;
//		if (null != party.getCorporation() && "0".equals(promotionParty.getCorporation())) {
//			promotionParty.setCorporation("1");
//			empiricValueAdded += 100;
//			PromotionRecord promotionRecord = new PromotionRecord();
//			promotionRecord.setUserId(party.getId() + "");
//			promotionRecord.setType(party.getPartyType());
//			promotionRecord.setEmpiricItem("法人代表信息");
//			promotionRecord.setAddedTime(PromotionUtil.getTime());
//			promotionRecord.setEmpiricValue(100);
//			promotionMapp.insertRecord(promotionRecord);
//		}
//		promotionMapp.updateParty(promotionParty);
//		promotion.setEmpiricValue(empiricValueAdded + promotion.getEmpiricValue());
//		promotionMapp.update(promotion);
//		// 记录增加的积分值
//		if (null != promotionPartyAdded && promotionPartyAdded.getId() != null) {
//			promotionPartyAdded.setEmpiricValueAdded(empiricValueAdded + promotionPartyAdded.getEmpiricValueAdded());
//			promotionMapp.updatePartyAdded(promotionPartyAdded);
//		} else {
//			promotionPartyAdded = new PromotionPartyAdded();
//			promotionPartyAdded.setMonth(month);
//			promotionPartyAdded.setUserId(String.valueOf(party.getId()));
//			promotionPartyAdded.setEmpiricValueAdded(empiricValueAdded);
//			promotionMapp.insertPartyAdded(promotionPartyAdded);
//		}
//    }

    @Override
    @Transactional
    public Map<String, Object> deleteById(String id) {
        try {
            partyMapp.deleteById(id);
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

    @Override
    public Map<String, Object> queryById(String id) {
        ExtParty systemAuthor = null;
        try {
            systemAuthor = partyMapp.queryById(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",systemAuthor);
    }

    /**
     * 根据机构的id查询当前机构下的机构列表
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> queryByIdList(String id) {
        List<Party> partyList = new ArrayList<Party>();
        try{
            partyList = partyMapp.queryByPartyIdList(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",partyList);
    }

    /**
     * 根据机构的父id和类型查询当前机构下的机构列表
     * @param party
     * @return
     */
    @Override
    public Map<String, Object> queryByIdAndTypeList(Party party) {
        List<Party> partyList = new ArrayList<Party>();
        try{
        	if(party.getId()!=null){
				partyList = partyMapp.queryByIdAndTypeList(party);
			} else if ("4".equals(party.getPartyType()) && "1".equals(party.getLastManageParty())) {
				partyList = partyMapp.queryByLastManageAndTypeList(party.getPartyType());
			} else {
				partyList = partyMapp.queryByTypeList(party.getPartyType());
			}

        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",partyList);
    }

	/**
	 * {label:'维保单位',value:'1'},
	 {label:'使用单位',value:'2'},
	 {label:'检验机构',value:'3'},
	 {label:'监管部门',value:'4'},
	 * 导入机构
	 * @param file
	 * @return
	 */
	@Override
	@Transactional
	public Map<String, Object> importExcelParty(MultipartFile file,String parentId) {
		try {
			List<List<Object>> list = ExcelUtil.getListByExcel(file.getInputStream(),file.getOriginalFilename());
			//获取导入的文件
			if(list!=null && list.size()>0){
				for(int i = 0;i < list.size();i++){
					List listObject = list.get(i);
					if(listObject != null && ("序号".equals(listObject.get(0)+""))){
						continue;
					}
					if(listObject != null && listObject.size() > 0){

						//插入使用单位

						if( listObject.size()>=2 && listObject.get(1)!=null && !"".equals(listObject.get(1))){
							Party selectParty = partyMapp.queryByName(listObject.get(1).toString());
							if(selectParty==null){
								Party party = new Party();
								party.setPartyType("2");
								party.setPartyName(listObject.get(1).toString());
								party.setParentId(parentId);
								partyMapp.insert(party);
//                                if ("1".equals(party.getPartyType()) || "2".equals(party.getPartyType())) {
//                                    Promotion promotion = new Promotion();
//                                    promotion.setType(party.getPartyType());
//                                    promotion.setUserId(party.getId() + "");
//                                    PromotionParty promotionParty = new PromotionParty();
//                                    promotionParty.setUserId(party.getId() + "");
//                                    promotionParty.setType(party.getPartyType());
//                                    Integer empiricValueAdded = 0;
//                                    if (null != party.getCorporation()) {
//                						promotionParty.setCorporation("1");
//                						empiricValueAdded += 100;
//                						PromotionRecord promotionRecord = new PromotionRecord();
//                						promotionRecord.setUserId(party.getId() + "");
//                						promotionRecord.setType(party.getPartyType());
//                						promotionRecord.setEmpiricItem("法人代表信息");
//                						promotionRecord.setAddedTime(PromotionUtil.getTime());
//                						promotionRecord.setEmpiricValue(100);
//                						promotionMapp.insertRecord(promotionRecord);
//                					}else{
//                                        promotionParty.setCorporation("0");
//                                    }
//                					if (null != party.getTelephone()) {
//                						promotionParty.setTelephone("1");
//                						empiricValueAdded += 100;
//                						PromotionRecord promotionRecord = new PromotionRecord();
//                						promotionRecord.setUserId(party.getId() + "");
//                						promotionRecord.setType(party.getPartyType());
//                						promotionRecord.setEmpiricItem("单位电话信息");
//                						promotionRecord.setAddedTime(PromotionUtil.getTime());
//                						promotionRecord.setEmpiricValue(100);
//                						promotionMapp.insertRecord(promotionRecord);
//                					}else{
//                                        promotionParty.setTelephone("0");
//                                    }
//                					if (null != party.getContacts()) {
//                						promotionParty.setContacts("1");
//                						empiricValueAdded += 100;
//                						PromotionRecord promotionRecord = new PromotionRecord();
//                						promotionRecord.setUserId(party.getId() + "");
//                						promotionRecord.setType(party.getPartyType());
//                						promotionRecord.setEmpiricItem("联系人信息");
//                						promotionRecord.setAddedTime(PromotionUtil.getTime());
//                						promotionRecord.setEmpiricValue(100);
//                						promotionMapp.insertRecord(promotionRecord);
//                					}else{
//                                        promotionParty.setContacts("0");
//                                    }
//                					if (null != party.getContactsWork()) {
//                						promotionParty.setContactsWork("1");
//                						empiricValueAdded += 100;
//                						PromotionRecord promotionRecord = new PromotionRecord();
//                						promotionRecord.setUserId(party.getId() + "");
//                						promotionRecord.setType(party.getPartyType());
//                						promotionRecord.setEmpiricItem("联系人职务信息");
//                						promotionRecord.setAddedTime(PromotionUtil.getTime());
//                						promotionRecord.setEmpiricValue(100);
//                						promotionMapp.insertRecord(promotionRecord);
//                					}else{
//                                        promotionParty.setContactsWork("0");
//                                    }
//                					if (null != party.getContactsCard()) {
//                						promotionParty.setContactsCard("1");
//                						empiricValueAdded += 100;
//                						PromotionRecord promotionRecord = new PromotionRecord();
//                						promotionRecord.setUserId(party.getId() + "");
//                						promotionRecord.setType(party.getPartyType());
//                						promotionRecord.setEmpiricItem("联系人身份证号码信息");
//                						promotionRecord.setAddedTime(PromotionUtil.getTime());
//                						promotionRecord.setEmpiricValue(100);
//                						promotionMapp.insertRecord(promotionRecord);
//                					}else{
//                                        promotionParty.setContactsCard("0");
//                                    }
//                					if (null != party.getContactsPhone()) {
//                						promotionParty.setContactsPhone("1");
//                						empiricValueAdded += 100;
//                						PromotionRecord promotionRecord = new PromotionRecord();
//                						promotionRecord.setUserId(party.getId() + "");
//                						promotionRecord.setType(party.getPartyType());
//                						promotionRecord.setEmpiricItem("联系人电话信息");
//                						promotionRecord.setAddedTime(PromotionUtil.getTime());
//                						promotionRecord.setEmpiricValue(100);
//                						promotionMapp.insertRecord(promotionRecord);
//                					}else{
//                                        promotionParty.setContactsPhone("0");
//                                    }
//                					if (null != party.getContactsEmile()) {
//                						promotionParty.setContactsEmile("1");
//                						empiricValueAdded += 100;
//                						PromotionRecord promotionRecord = new PromotionRecord();
//                						promotionRecord.setUserId(party.getId() + "");
//                						promotionRecord.setType(party.getPartyType());
//                						promotionRecord.setEmpiricItem("联系人邮箱信息");
//                						promotionRecord.setAddedTime(PromotionUtil.getTime());
//                						promotionRecord.setEmpiricValue(100);
//                						promotionMapp.insertRecord(promotionRecord);
//                					}else{
//                                        promotionParty.setContactsEmile("0");
//                                    }
//                					if (null != party.getContactsMobile()) {
//                						promotionParty.setContactsMobile("1");
//                						empiricValueAdded += 100;
//                						PromotionRecord promotionRecord = new PromotionRecord();
//                						promotionRecord.setUserId(party.getId() + "");
//                						promotionRecord.setType(party.getPartyType());
//                						promotionRecord.setEmpiricItem("联系人手机信息");
//                						promotionRecord.setAddedTime(PromotionUtil.getTime());
//                						promotionRecord.setEmpiricValue(100);
//                						promotionMapp.insertRecord(promotionRecord);
//                					}else{
//                                        promotionParty.setContactsMobile("0");
//                                    }
//                					if (null != party.getContactsAddress()) {
//                						promotionParty.setContactsAddress("1");
//                						empiricValueAdded += 100;
//                						PromotionRecord promotionRecord = new PromotionRecord();
//                						promotionRecord.setUserId(party.getId() + "");
//                						promotionRecord.setType(party.getPartyType());
//                						promotionRecord.setEmpiricItem("联系人地址信息");
//                						promotionRecord.setAddedTime(PromotionUtil.getTime());
//                						promotionRecord.setEmpiricValue(100);
//                						promotionMapp.insertRecord(promotionRecord);
//                					}else{
//                                        promotionParty.setContactsAddress("0");
//                                    }
//                                    promotionMapp.insertParty(promotionParty);
//                                    promotion.setEmpiricValue(empiricValueAdded);
//                                    promotionMapp.insert(promotion);
//                                    // 记录增加的积分值
//                                    PromotionPartyAdded promotionPartyAdded = new PromotionPartyAdded();
//                                    promotionPartyAdded.setMonth(new SimpleDateFormat("yyyyMM").format(new Date()));
//                                    promotionPartyAdded.setUserId(party.getId() + "");
//                                    promotionPartyAdded.setEmpiricValueAdded(empiricValueAdded);
//                                    promotionMapp.insertPartyAdded(promotionPartyAdded);
//                                }

							}

						}
						//插入维保单位
						if( listObject.size()>=3 && listObject.get(2)!=null && !"".equals(listObject.get(2))) {
							Party selectParty = partyMapp.queryByName(listObject.get(2).toString());
							if(selectParty==null) {
								Party party = new Party();
								party.setPartyType("1");
								party.setPartyName(listObject.get(2).toString());
								party.setParentId(parentId);
								partyMapp.insert(party);
//                                if ("1".equals(party.getPartyType()) || "2".equals(party.getPartyType())) {
//                                    Promotion promotion = new Promotion();
//                                    promotion.setType(party.getPartyType());
//                                    promotion.setUserId(party.getId() + "");
//                                    PromotionParty promotionParty = new PromotionParty();
//                                    promotionParty.setUserId(party.getId() + "");
//                                    promotionParty.setType(party.getPartyType());
//                                    Integer empiricValueAdded = 0;
//                                    if (null != party.getCorporation()) {
//                                        promotionParty.setCorporation("1");
//                                        empiricValueAdded += 100;
//                                    } else {
//                                        promotionParty.setCorporation("0");
//                                    }
//                                    if (null != party.getTelephone()) {
//                                        promotionParty.setTelephone("1");
//                                        empiricValueAdded += 100;
//                                    } else {
//                                        promotionParty.setTelephone("0");
//                                    }
//                                    if (null != party.getContacts()) {
//                                        promotionParty.setContacts("1");
//                                        empiricValueAdded += 100;
//                                    } else {
//                                        promotionParty.setContacts("0");
//                                    }
//                                    if (null != party.getContactsWork()) {
//                                        promotionParty.setContactsWork("1");
//                                        empiricValueAdded += 100;
//                                    } else {
//                                        promotionParty.setContactsWork("0");
//                                    }
//                                    if (null != party.getContactsCard()) {
//                                        promotionParty.setContactsCard("1");
//                                        empiricValueAdded += 100;
//                                    } else {
//                                        promotionParty.setContactsCard("0");
//                                    }
//                                    if (null != party.getContactsPhone()) {
//                                        promotionParty.setContactsPhone("1");
//                                        empiricValueAdded += 100;
//                                    } else {
//                                        promotionParty.setContactsPhone("0");
//                                    }
//                                    if (null != party.getContactsEmile()) {
//                                        promotionParty.setContactsEmile("1");
//                                        empiricValueAdded += 100;
//                                    } else {
//                                        promotionParty.setContactsEmile("0");
//                                    }
//                                    if (null != party.getContactsMobile()) {
//                                        promotionParty.setContactsMobile("1");
//                                        empiricValueAdded += 100;
//                                    } else {
//                                        promotionParty.setContactsMobile("0");
//                                    }
//                                    if (null != party.getContactsAddress()) {
//                                        promotionParty.setContactsAddress("1");
//                                        empiricValueAdded += 100;
//                                    } else {
//                                        promotionParty.setContactsAddress("0");
//                                    }
//                                    promotionMapp.insertParty(promotionParty);
//                                    promotion.setEmpiricValue(empiricValueAdded);
//                                    promotionMapp.insert(promotion);
//                                    // 记录增加的积分值
//                                    PromotionPartyAdded promotionPartyAdded = new PromotionPartyAdded();
//                                    promotionPartyAdded.setMonth(new SimpleDateFormat("yyyyMM").format(new Date()));
//                                    promotionPartyAdded.setUserId(party.getId() + "");
//                                    promotionPartyAdded.setEmpiricValueAdded(empiricValueAdded);
//                                    promotionMapp.insertPartyAdded(promotionPartyAdded);
//                                }
							}
						}
						//插入检验机构
						if( listObject.size()>=4 && listObject.get(3)!=null && !"".equals(listObject.get(3))) {
							Party selectParty = partyMapp.queryByName(listObject.get(3).toString());
							if(selectParty==null) {
								Party party3 = new Party();
								party3.setPartyType("3");
								party3.setPartyName(listObject.get(3).toString());
								party3.setParentId(parentId);
								partyMapp.insert(party3);
							}
						}
					}
				}
			}
		}catch (Exception e){
			logger.error(e.getMessage(),e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return ResultUtil.returnError(e.getMessage(),null);
		}
		return ResultUtil.returnSuccess("成功",null);
	}

	/**
	 * {label:'维保单位',value:'1'},
	 {label:'使用单位',value:'2'},
	 {label:'检验机构',value:'3'},
	 {label:'监管部门',value:'4'},
	 * 根据机构来提示机构需要完善的信息
	 * @param partyId
	 * @return
	 */
	@Override
	public Map<String, Object> checkParytIdMessage(String partyId) {
		/** 查看机构下是否有用户如果没有积极提示
		 *  电梯安全管理人员档案》未完善，贵单位员工不能获得注册权限，是否立刻完善？
		 */
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			List<PartyUser> list = partyUserMapp.findByPartyIdAndType(partyId);
			if(list==null || list.size()<=0){
				map.put("user","电梯安全管理人员档案》未完善，贵单位员工不能获得注册权限，是否立刻完善");
			}
			//如果是监管部门还需要判断主题责任名册附件一
			ExtParty party = partyMapp.queryById(partyId);
			if(party!=null && party.getPartyType().equals("4")){
				List<Party> partyList = partyMapp.queryByPartyIdList(partyId);
				if(partyList==null||partyList.size()<=0){
					map.put("party","请完善主题责任名册附件一");
				}
			}
			//维保单位 使用单位都有合同
			if(party!=null && (party.getPartyType().equals("1")||party.getPartyType().equals("2"))){
				List<PartyContract> partyContract = partyContractMapp.queryByPartyId(partyId);
				if(partyContract==null||partyContract.size()<=0){
					map.put("partyContract","请完善合同");
				}
			}
			//使用单位还有电梯台帐 插入电梯表
			if(party!=null && party.getPartyType().equals("2")){
				List<Elevator> elevatorList = elevatorMapp.queryByUsedPartyId(partyId);
				if(elevatorList==null||elevatorList.size()<=0){
					map.put("elevator","请完善电梯台帐");
				}
			}
			// 判断分局还是局
			String type = decideInfo(partyId);
			map.put("type",type);


		}catch (Exception e){
			logger.error(e.getMessage(),e);
			return ResultUtil.returnError(e.getMessage(),null);
		}
		return ResultUtil.returnSuccess("成功",map);
	}

	@Override
	public Map<String, Object> queryByNameLk(Map map) {
		try {
			String name = "%"+map.get("partyName")+"%";
			List<Party> pl = partyMapp.queryByNameLk(name);
			if(pl!=null && pl.size()==1){
				return ResultUtil.returnSuccess("查到一家",pl);
			}else if(pl!=null && pl.size()>1){
				return ResultUtil.returnSuccess("根据单位名查到多家单位",pl);
			}else{
				return ResultUtil.returnSuccess("该单位在系统中不存在",null);
			}

		}catch (Exception e){
			logger.error("模糊查询机构失败："+e.getMessage(),e);
			return ResultUtil.returnError("根据单位名查查询出错了，请重试",null);
		}
	}

	/**
	 * 获取某级别所有的监管单位
	 * @param map
	 * @return
	 */
	@Override
	public Map<String, Object> queryPartyByPartyLevel(Map map) {
		List<Party> partyList = new ArrayList<Party>();
		try{
			if(map.get("partyLevel") != null && !"".equals(map.get("partyLevel"))){
				partyList = partyMapp.queryPartyByPartyLevel(map.get("partyLevel")+"");
			}else{
				return ResultUtil.returnError("参数为空",null);
			}

		}catch (Exception e){
			logger.error(e.getMessage(),e);
			return ResultUtil.returnError(e.getMessage(),null);
		}
		return ResultUtil.returnSuccess("成功",partyList);
	}

	/**
	 * 获取是否是分局信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	private String decideInfo(String id) {
		String limit = "0";
		// 判断市或者局  通过传入Id判断  0是市级 1是局级 2 是分局
		List<Party> partys = partyMapp.decideBranchInfo(id);
		if (partys != null && partys.size() > 0) {
			List<Party> partyList = partyMapp.decideForEach(id);
			if (partyList != null && partyList.size() > 0) {
				limit = "0";
			} else {
				// 此时为局
				limit = "1";
			}
		} else {
			limit = "2";
		}
		return limit;
	}
}
